﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ywxt.NewsPush.Controllers
{
    using NHibernate;
    using NHibernate.Criterion;
    using DB_NH;
    using System.IO;

    /// <summary>
    /// 公用控制器
    /// </summary>
    public class CommonController : Controller
    {
        //
        // GET: /Common/


        /// <summary>
        /// 项目列表的信息
        /// </summary>
        public class ProjectInfo
        {
            public string id { get; set; }
            public string name { get; set; }
        }

        /// <summary>
        /// 根据项目代码或名称，获取项目列表
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public JsonResult GetProjects(string keyword)
        {
            var sessionFactory = SessionFactory.GetCurrentFactory();
            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    #region 获取项目信息
                    string ss = keyword;
                    if (keyword.IndexOf('/') >= 0)
                    {
                        ss = keyword.Substring(0, keyword.IndexOf('/'));
                    }

                    var _projects = session.QueryOver<DB_NH.DB_Model.ywxt.t_project>()
                        .Where(c => c.is_valid == 1 && c.is_leaf == 1 && (
                            c.proj_code.IsLike("%" + ss) || c.proj_code.IsLike("%" + ss + "/%") || c.proj_name.IsLike("%" + ss + "%")))
                        .OrderBy(c => c.proj_code).Asc()
                        .Take(10)
                        .List();

                    List<ProjectInfo> projects = new List<ProjectInfo>();

                    foreach (var _e in _projects)
                    {
                        string _proj_code = _e.proj_code;
                        if (_proj_code.IndexOf('/') >= 0)
                        {
                            //项目编号带有“/”，则编号截取第一个"/"字符前5位
                            _proj_code = _proj_code.Remove(0, _proj_code.IndexOf('/') - 5);
                        }
                        else
                        {
                            _proj_code = _proj_code.Remove(0, _proj_code.Length - 5);
                        }

                        _proj_code += " " + _e.proj_name;

                        ProjectInfo _p = new ProjectInfo()
                        {
                            id = _e.proj_id,
                            name = _proj_code
                        };

                        projects.Add(_p);
                    }

                    transaction.Commit();
                    return Json(projects);
                    #endregion
                }
            }
        }

        /// <summary>
        /// 专家专业列表的信息
        /// </summary>
        public class ProfessionalInfo
        {
            public string id { get; set; }
            public string name { get; set; }
        }

        /// <summary>
        /// 根据专业名称，获取专家专业列表
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public JsonResult GetExpertProfessionals(string keyword)
        {
            var sessionFactory = SessionFactory.GetCurrentFactory();
            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    #region 获取项目信息
                    var _professionals = session.QueryOver<DB_NH.DB_Model.ZJK.ZJ_expert>()
                        .Select(Projections.Group<DB_NH.DB_Model.ZJK.ZJ_expert>(p => p.zj_professional))
                        .OrderBy(c => c.zj_professional).Asc()
                        .Take(10).List<string>();

                    List<ProfessionalInfo> professionals = new List<ProfessionalInfo>();

                    foreach (var _e in _professionals)
                    {
                        ProfessionalInfo _p = new ProfessionalInfo()
                        {
                            id = _e,
                            name = _e
                        };

                        professionals.Add(_p);
                    }

                    transaction.Commit();
                    return Json(professionals);
                    #endregion
                }
            }
        }

        /// <summary>
        /// 专家分类列表的信息
        /// </summary>
        public class CategoryInfo
        {
            public string id { get; set; }
            public string name { get; set; }
        }

        /// <summary>
        /// 根据分类名称，获取分类列表
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public JsonResult GetExpertCategorys(string keyword)
        {
            var sessionFactory = SessionFactory.GetCurrentFactory();
            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    #region 获取项目信息
                    var _categorys = session.QueryOver<DB_NH.DB_Model.ZJK.ZJ_category>()
                        .Where(c => c.IsValid == 1 && c.is_leaf == 1 && c.category_name.IsLike("%" + keyword + "%"))
                        .OrderBy(c => c.category_name).Asc()
                        .Take(10).List();

                    List<CategoryInfo> category_infos = new List<CategoryInfo>();

                    foreach (var _e in _categorys)
                    {
                        CategoryInfo _p = new CategoryInfo()
                        {
                            id = _e.Id.ToString(),
                            name = _e.category_name
                        };

                        category_infos.Add(_p);
                    }

                    transaction.Commit();
                    return Json(category_infos);
                    #endregion
                }
            }
        }
    }
}
